module top_module(
    input clk,
    input areset,    // Freshly brainwashed Lemmings walk left.
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output walk_left,
    output walk_right,
    output aaah,
    output digging ); 

    parameter LEFT = 3'b000;
    parameter RIGHT = 3'b001;
    parameter FALL_L = 3'b010;
    parameter FALL_R = 3'b011;
    parameter DIG_L = 3'b100;
    parameter DIG_R = 3'b101;
    
    reg	[2:0]	state;
    reg	[2:0]	next_state;
    
    always @(*) begin
        case(state)
            LEFT:begin
                if(!ground) begin
                    next_state = FALL_L;
                end
                else begin
                    if(dig) begin
                        next_state = DIG_L;
                    end
                    else if(bump_left) begin
                        next_state = RIGHT;
                    end
                    else begin
                        next_state = LEFT;
                    end
                end
            end
            RIGHT:begin
                if(!ground) begin
                    next_state = FALL_R;
                end
                else begin
                    if(dig) begin
                        next_state = DIG_R;
                    end
                    else if(bump_right) begin
                        next_state = LEFT;
                    end
                    else begin
                        next_state = RIGHT;
                    end
                end
            end
            FALL_L:begin
                if(!ground) begin
                    next_state = FALL_L;
                end
                else begin
                    next_state = LEFT;
                end
            end
            FALL_R:begin
                if(!ground) begin
                    next_state = FALL_R;
                end
                else begin
                    next_state = RIGHT;
                end
            end
            DIG_L:begin
                if(!ground) begin
                    next_state = FALL_L;
                end
                else begin
                    next_state = DIG_L;
                end
            end
            DIG_R:begin
                if(!ground) begin
                    next_state = FALL_R;
                end
                else begin
                    next_state = DIG_R;
                end
            end
            default:next_state = next_state;
        endcase
    end
    
    always @(posedge clk or posedge areset) begin
        if(areset) begin
            state <= LEFT;
        end
        else begin
            state <= next_state;
        end
    end
    
    assign walk_left = (state == LEFT);
    assign walk_right = (state == RIGHT);
    assign aaah = (state == FALL_L) || (state == FALL_R);
    assign digging = (state == DIG_L) || (state == DIG_R);
    
endmodule
